package Str;

/**
 * @auther wangchenghao
 * @date 2021/6/14
 **/
public class test9 {
    public static String longestCommonPrefix(String[] strs) {
//边界条件判断
        if (strs == null || strs.length == 0) {
            return "";
        }
        //默认第一个字符串是他们的公共前缀
        String pre = strs[0];
        int i = 1;
        while (i < strs.length) {
            //不断的截取
            while (strs[i].indexOf(pre) != 0) {
                pre = pre.substring(0, pre.length() - 1);
            }
            i++;
        }
        return pre;

    }

    public static String getMaxString(String a, String b) {
        int i = 0;
        int j = 0;
        int length = 0;
        String result = "";
        for (; i < a.length() && j < b.length(); i++, j++) {
            if (a.charAt(i) == b.charAt(j)) {
                result += String.valueOf(a.charAt(i));
                length++;
            } else {
                result = "";
                length = 0;
            }
        }

        if (i == 0 || j == 0) {
            return "";
        } else {
            return result;
        }
    }

    public static void main(String[] args) {
        String[] strings = new String[]{"1152244", "113358"};
        System.out.println("longestCommonPrefix(strings) = " + longestCommonPrefix(strings));
    }
}
